package cn.iocoder.yudao.module.of.controller.admin.schedulecenter;

import cn.iocoder.yudao.module.of.controller.admin.taskinfo.vo.TaskInfoRespVO;
import cn.iocoder.yudao.module.of.enums.BoardGropEnum;
import cn.iocoder.yudao.module.of.enums.TimeTypeConstants;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.of.controller.admin.schedulecenter.vo.*;
import cn.iocoder.yudao.module.of.service.schedulecenter.ScheduleCenterService;

@Log4j2
@Tag(name = "管理后台 - 日程中心 信息")
@RestController
@RequestMapping("/of/schedule-center")
@Validated
public class ScheduleCenterController {

    @Resource
    private ScheduleCenterService scheduleCenterService;

    @PostMapping("/create")
    @Operation(summary = "创建日程中心 信息")
   // @PreAuthorize("@ss.hasPermission('of:schedule-center:create')")
    public CommonResult<Long> createScheduleCenter(@Valid @RequestBody ScheduleCenterSaveReqVO createReqVO) {
        return success(scheduleCenterService.createScheduleCenter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新日程中心 信息")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:update')")
    public CommonResult<Boolean> updateScheduleCenter(@Valid @RequestBody ScheduleCenterSaveReqVO updateReqVO) {
        scheduleCenterService.updateScheduleCenter(updateReqVO);
        return success(true);
    }


    @PostMapping("/userTree")
    @Operation(summary = "获取用户树")
   // @PreAuthorize("@ss.hasPermission('system:user:query')")
    public CommonResult<List> userTree( @RequestBody ScheduleCenterReqVO reqVO) {

        List list = null;
        if(reqVO.getDeptId() != null){
            list = scheduleCenterService.getSubDeptAndUser(reqVO.getDeptId());
        }else{
            list = scheduleCenterService.userTree(reqVO.getUserList());
        }
        return success(list);
    }

    @GetMapping("/getSubDeptAndUser")
    @Operation(summary = "新增用户")
    // @PreAuthorize("@ss.hasPermission('system:user:query')")
    public CommonResult<List> getSubDeptAndUser( @RequestParam Long deptId) {
        List id = scheduleCenterService.getSubDeptAndUser(deptId);
        return success(id);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除日程中心 信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('of:schedule-center:delete')")
    public CommonResult<Boolean> deleteScheduleCenter(@RequestParam("id") Long id) {
        scheduleCenterService.deleteScheduleCenter(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得日程中心 信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:query')")
    public CommonResult<ScheduleCenterRespVO> getScheduleCenter(@RequestParam("id") Long id) {
        ScheduleCenterRespVO scheduleCenter = scheduleCenterService.getScheduleCenter(id);
        return success(scheduleCenter);
    }

    @GetMapping("/getTaskById")
    @Operation(summary = "获得日程中心 信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:query')")
    public CommonResult<TaskInfoRespVO> getTaskInfo(@RequestParam("id") Long id) {
        TaskInfoRespVO taskRespVO = scheduleCenterService.getTaskInfo(id);
        return success(taskRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得日程中心 信息分页")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:query')")
    public CommonResult<PageResult<ScheduleCenterRespVO>> getScheduleCenterPage(@Valid ScheduleCenterPageReqVO pageReqVO) {
        PageResult<ScheduleCenterRespVO> pageResult = scheduleCenterService.getScheduleCenterPage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/list")
    @Operation(summary = "获得日程中心 日历信息")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:query')")
    public Object getScheduleCenterList(@RequestBody ScheduleCenterPageReqVO pageReqVO) {
        log.info("repage:::"+JSONObject.toJSONString(pageReqVO));
        //日 周 视图数据
        if(pageReqVO.getTimeType() ==  TimeTypeConstants.QUERY_PERIOD_DAY ||
                pageReqVO.getTimeType() ==  TimeTypeConstants.QUERY_PERIOD_WEEK) {
            JSONObject listGroupDay = scheduleCenterService.getListGroupDay(pageReqVO);
            return success(listGroupDay) ;
        }
        //返回月 年数据
        List<ScheduleCenterRespVO> list = scheduleCenterService.getScheduleCenterList(pageReqVO);
        return success(list);
    }




    @PostMapping("/listByGroup")
    @Operation(summary = "获得日程中心")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:query')")
    public CommonResult<JSONObject> getListGroupDay(@RequestBody ScheduleCenterPageReqVO reqVo) {
        JSONObject listGroupDay = scheduleCenterService.getListGroupDay(reqVo);
        return success(listGroupDay);
    }

    @PostMapping("/board/list")
    @Operation(summary = "获得日程中心 看板信息")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:query')")
    public CommonResult<JSONObject> getBoardList(@RequestBody ScheduleCenterBordReqVo reqVo) {

        JSONObject list = scheduleCenterService.getScheduleCenterBoradList(reqVo);
        return success(list);
    }

    @PostMapping("/task/list")
    @Operation(summary = "获得 全年未完成任务")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:query')")
    public CommonResult<List> getAllYearTaskList(@RequestBody ScheduleCenterBordReqVo reqVo) {

        List allYearTaskList = scheduleCenterService.getAllYearTaskList(reqVo);
        return success(allYearTaskList);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出日程中心 信息 Excel")
    @PreAuthorize("@ss.hasPermission('of:schedule-center:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScheduleCenterExcel(@Valid ScheduleCenterPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScheduleCenterRespVO> list = scheduleCenterService.getScheduleCenterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "日程中心 信息.xls", "数据", ScheduleCenterRespVO.class,
                list);
    }

}